package chloroform

import _root_.argo.jdom.{JsonRootNode, JdomParser}
import javax.servlet.ServletConfig
import java.lang.String
import javax.servlet.http.{HttpSession, HttpServletRequest, HttpServletResponse, HttpServlet}
import java.io.PrintWriter
import java.util.{Locale, Enumeration}
import _root_.util.SerializationUtils._
import util.JsonUtils

/**
 * <p>
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: Nov 12, 2010
 * Time: 6:58:02 PM
 */

class CookieServlet extends HttpServlet {
//  private val COOKIE_SESSION_ATTR: String = classOf[StarterConfigMessageHandler].getName
//
////  val refMap: Map[String, Map[Class[_], Class[Starter[_]]]] =
////  Map("ref1" -> Map(classOf[String] -> classOf[wwwstarters.OnePhaseStarter].asInstanceOf[Class[Starter[_]]]),
////    "ref2" -> Map(classOf[String] -> classOf[wwwstarters.TwoPhaseStarter].asInstanceOf[Class[Starter[_]]]))
////  implicit val refMapFn = new MapConfigPageLookup(refMap)
//
//  override def init(config: ServletConfig) = {
//    super.init(config)
//  }
//
//  def swapSession(session: StarterConfigMessageHandler, httpSession: HttpSession): StarterConfigMessageHandler = {
//    val swapped = deserialize(serialize(session)).asInstanceOf[StarterConfigMessageHandler]
//    httpSession.setAttribute(COOKIE_SESSION_ATTR, swapped)
//    swapped
//  }
//
//  override def doGet(req: HttpServletRequest, resp: HttpServletResponse) = {
//    // todo: the GET request is supposed to carry the message as the request parameters
//
//    val httpSession = req.getSession
//
//    httpSession.synchronized {
//      val configSession = getConfigSession(req, req.getLocale)
//      val methodName = req.getParameter("method")
//
//      val parameterNames: Enumeration[String] = req.getParameterNames.asInstanceOf[Enumeration[String]]
//      var argMap: Map[Int, String] = Map.empty
//      while (parameterNames.hasMoreElements) {
//        val param = parameterNames.nextElement
//        if (param.startsWith("arg")) {
//          argMap += (Integer.parseInt(param.substring(3)) -> req.getParameter(param))
//        }
//      }
//      val argArray: Array[AnyRef] = new Array(argMap.size)
//      argMap.foreach(pair => argArray(pair._1) = pair._2)
//
//      classOf[StarterConfigMessageHandler].getMethods.find(_.getName == methodName) match {
//        case None => {
//          resp.setContentType("text/plain")
//          val printWriter: PrintWriter = resp.getWriter
//          printWriter.write("Unknown method " + methodName)
//        }
//        case Some(method) => {
//          val renderer: Renderer = method.invoke(configSession, argArray: _*).asInstanceOf[Renderer]
//          JsonUtils.renderResponse(resp, renderer)
//        }
//      }
//
//    }
//  }
//
//  def getConfigSession(request: HttpServletRequest, locale: Locale): StarterConfigMessageHandler = {
//    val sessionAttr: StarterConfigMessageHandler = request.getSession().
//            getAttribute(COOKIE_SESSION_ATTR).asInstanceOf[StarterConfigMessageHandler]
//    if (sessionAttr == null || sessionAttr.isFinished) {
//      val page = ConfigPageRegisterLookup(request)
//      val s = new StarterConfigMessageHandler(page, locale)
//      request.getSession().setAttribute(COOKIE_SESSION_ATTR, s)
//      s
//    } else
//      sessionAttr
//  }
//
//  override def doPost(req: HttpServletRequest, resp: HttpServletResponse) = {
//    // the POST request is supposed to carry a JSON message
//
//    val httpSession = req.getSession
//
//    httpSession.synchronized {
//
//      var configSession = getConfigSession(req, req.getLocale)
//
//      val responseCookie: JsonRootNode = JsonUtils.parser.parse(req.getReader)
//      val renderer: Renderer = configSession.processResponse(responseCookie)
//      JsonUtils.renderResponse(resp, renderer)
//
//      configSession = swapSession(configSession, httpSession)
//
//      if (configSession.isFinished) {
//        httpSession.removeAttribute(COOKIE_SESSION_ATTR)
//      }
//
//    }
//  }

}